﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.ComponentModel;
using Macad.Occt;
using Microsoft.Extensions.DependencyInjection;
using SpCad;
using SPCAM.Plugin.Cam.Model;
using SPCAM.Plugin.Cam.ViewModel;
using SPCAM.Plugin.Cam.Views;
using static Macad.Interaction.ToggleSubshapesAction;

namespace SPCAM.Plugin.Cam.Servers
{
    public class WeldServer : ObservableObject
    {
        private static WeldServer _server;
        public static WeldServer Get => _server ?? (_server = new WeldServer());
        private ObservableCollection<WeldTreeModel> _TreeNode = new ObservableCollection<WeldTreeModel>();
        /// <summary>
        /// 边框属性窗口
        /// </summary>
        private UEdgeProperty uEdgeProperty = new UEdgeProperty();
        public ObservableCollection<WeldTreeModel> TreeNode { get => _TreeNode;set=>SetProperty(ref _TreeNode,value); }
        public ObservableCollection<TopoDS_Edge> SelectEdges { get; set; } = new ObservableCollection<TopoDS_Edge>();
    
        public WeldServer()
        {
            TreeNode.CollectionChanged += TreeNode_CollectionChanged;
            SelectEdges.CollectionChanged += SelectEdges_CollectionChanged; 
        }

      

        /// <summary>
        /// 当添加焊缝时候添加到焊缝列表Node
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SelectEdges_CollectionChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
 
            switch (e.Action)
            {
                case System.Collections.Specialized.NotifyCollectionChangedAction.Add:
                    var shapeadd = (TopoDS_Edge)e.NewItems[0];
                    if (shapeadd != null)
                    {


                        TreeNode.Add(new WeldTreeModel(shapeadd));
                        var tvm = App.Current.Services.GetService<WeldTreeViewModel>();
                        tvm.SelectWeld = TreeNode.Last();
                    }

                    break;
                case System.Collections.Specialized.NotifyCollectionChangedAction.Remove:
                    var shapeRem = (TopoDS_Edge)e.OldItems[0];
                    var node = TreeNode.First(c => c.TopoDSEdge.Equals(shapeRem));
                    if (shapeRem != null)
                    {
                        TreeNode.Remove(node);
                        if (TreeNode.Count == 0)
                        {
                            var tvm = App.Current.Services.GetService<WeldTreeViewModel>();
                            tvm.SelectWeld = null;
                        }
                    }
                    break;
                case System.Collections.Specialized.NotifyCollectionChangedAction.Replace:
                    break;
                case System.Collections.Specialized.NotifyCollectionChangedAction.Move:
                    break;
                case System.Collections.Specialized.NotifyCollectionChangedAction.Reset:
                    TreeNode.Clear();
                    break;
                default:
                    break;
            }

        }
        bool isShowPanel = false;
        /// <summary>
        /// 如果焊缝数量大于1在属性窗口添加一个属性窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TreeNode_CollectionChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            var shend=(ObservableCollection<WeldTreeModel>)sender!;
            if (shend!=null)
            {
                if (shend?.Count > 0)
                {
                    if (!isShowPanel)
                    {
                        SpServer.Instance.Current.PropertyPanelManager.AddPanel(uEdgeProperty, 100360);
                        isShowPanel = true;
                    }
                 
                }
                else
                {
                    isShowPanel = false;
                    SpServer.Instance.Current.PropertyPanelManager.RemovePanel(uEdgeProperty);
                }
            }
            
        }
    }
}
